package com.cooper.controllers.admin;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.cooper.constants.AppConstants;
import com.cooper.constants.DictConstants;
import com.cooper.controllers.Controller;
import com.cooper.controllers.Template;
import com.cooper.core.JsonResult;
import com.cooper.entity.Dict;
import com.cooper.entity.DisplayPicConfigListDo;
import com.cooper.entity.DisplaySelectConfigListDo;
import com.cooper.entity.UserInfo;
import com.cooper.pagination.PageInfo;
import com.cooper.pagination.PageResult;
import com.cooper.service.admin.DisplayPicConfigService;
import com.cooper.service.admin.LocationService;
import com.cooper.service.admin.MallCategoryService;
import com.cooper.service.core.DictService;
import com.cooper.util.Controllers;
import com.cooper.util.StringUtils;
import com.cooper.vo.DisplayPicConfigVo;
import com.cooper.vo.LocationVo;
import com.cooper.vo.MallCategoryVo;

import net.paoding.rose.web.Invocation;
import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.portal.Portal;

public class DisplayPicConfigListController extends Controller {

	@Autowired
	DictService dictService;
	
	@Autowired
	MallCategoryService mallCategoryService;
	
	@Autowired
	LocationService locationService;
	
	@Autowired
	private DisplayPicConfigService displayPicConfigService;
	
	@Template(title="轮播图片配置")
	@Get @Post
	public String content(Invocation inv, Portal portal, PageInfo pageInfo,DisplayPicConfigVo queryParamVo,@Param("toDisType") String toDisType) throws Exception{
		PageResult<DisplayPicConfigVo> ret = displayPicConfigService.queryDisplayPicConfigPage(queryParamVo, pageInfo);
		if("categoryDisConfig".equals(toDisType)){
			MallCategoryVo vo = new MallCategoryVo();
			vo.setParentId(1L);
			List<MallCategoryVo> data = mallCategoryService.findMallCategorys(vo);
			List<Dict> finalRet =  new ArrayList<Dict>();
			if(data!=null&&!data.isEmpty()){
				for(MallCategoryVo mvo:data){
					Dict dict = new Dict();
					dict.setDictName(mvo.getName());
					dict.setDictValue("mallcategory"+mvo.getId());
					finalRet.add(dict);
				}
			}
			inv.addModel("distTypes", finalRet);
		}else if("locationConfig".equals(toDisType)){
			List<Dict> finalRet =  new ArrayList<Dict>();
			List<LocationVo>  locationRet = locationService.getAllLocationWithChilds(LANG);
			if(locationRet!=null&&!locationRet.isEmpty()){
				for(LocationVo lo:locationRet){
					Dict dict = new Dict();
					dict.setDictName(lo.getZhCn());
					dict.setDictValue("location"+lo.getId());
					finalRet.add(dict);
				}
				inv.addModel("distTypes", finalRet);
			}
		}else{
			inv.addModel("distTypes", dictService.getDictList(DictConstants.DIS_TYPE));
		}
		inv.addModel("toDisType", StringUtils.isNotEmpty(toDisType)?toDisType:"disType");
		inv.addModel("toAddType", StringUtils.isNotEmpty(toDisType)?toDisType:"disType");
		inv.addModel("entryList", ret.getResult());
		inv.addModel(AppConstants.pageInfo, pageInfo);
 		inv.addModel("queryParamVo", queryParamVo);
		return "DisplayPicConfigList";
	}
	
	@Template(title="轮播图片添加")
	@Get("toAdd")
	public String toAdd(Invocation inv, Portal portal, @Param("id") Long id,@Param("toAddType") String toAddType) throws Exception{
		if("categoryDisConfig".equals(toAddType)){
			MallCategoryVo vo = new MallCategoryVo();
			vo.setParentId(1L);
			List<MallCategoryVo> ret = mallCategoryService.findMallCategorys(vo);
			List<Dict> finalRet =  new ArrayList<Dict>();
			if(ret!=null&&!ret.isEmpty()){
				for(MallCategoryVo mvo:ret){
					Dict dict = new Dict();
					dict.setDictName(mvo.getName());
					dict.setDictValue("mallcategory"+mvo.getId());
					finalRet.add(dict);
				}
			}
			inv.addModel("distTypes", finalRet);
		}else if("locationConfig".equals(toAddType)){
			List<LocationVo>  locationRet = locationService.getAllLocationWithChilds(LANG);
			if(locationRet!=null&&!locationRet.isEmpty()){
				List<Dict> finalRet =  new ArrayList<Dict>();
				for(LocationVo lo:locationRet){
					Dict dict = new Dict();
					dict.setDictName(lo.getZhCn());
					dict.setDictValue("location"+lo.getId());
					finalRet.add(dict);
				}
				inv.addModel("distTypes", finalRet);
			}
		}else{
			inv.addModel("distTypes", dictService.getDictList(DictConstants.DIS_TYPE));
		}
		inv.addModel("toAddType", StringUtils.isNotEmpty(toAddType)?toAddType:"disType");
		if(!StringUtils.isEmpty(id)){
			DisplayPicConfigListDo config = this.displayPicConfigService.findDisplayPicConfigById(id);
			inv.addModel("entity", config);
		}
		return "DisplayPicConfigAdd";
	}
	
	@Post("doAdd")
	public Object doAddConfig(Invocation inv,DisplayPicConfigListDo config){
		JsonResult json = new JsonResult();
		UserInfo userInfo = Controllers.getUserInfo(inv);
		try{
			if(StringUtils.isEmpty(config.getDisType()) || StringUtils.isEmpty(config.getFileId())){
				json.setSuccess(false);
				json.setMessage("场景和附件图片不能为空");
			} else {
				boolean bUpdate = !StringUtils.isEmpty(config.getId());
				Controllers.prepareEntityForDataSaving(config, userInfo, bUpdate);
				if(bUpdate){
					this.displayPicConfigService.updateDisplayPicConfig(config);
				} else {
					this.displayPicConfigService.addDisplayPicConfig(config);
				}
				json.setSuccess(true);
			}
		}catch(Exception e){
			json.setSuccess(false);
		}
		
		return json;
	}
	
	@Post("doRemove")
	public Object doRemove(Invocation inv,@Param("id") Long id){
		JsonResult json = new JsonResult();
		json.setSuccess(true);
		try{
			if(id!=null){
				displayPicConfigService.remove(id);
			}
		}catch(Exception e){
			json.setSuccess(false);
		}
		
		return json;
	}
	
	
	@Post("doCheck")
	public Object doCheck(Invocation inv,@Param("id") Long id,@Param("status") Integer status){
		JsonResult json = new JsonResult();
		try{
			if(!(id==null)&&!(null==status)){
				DisplayPicConfigListDo configDo = displayPicConfigService.findDisplayPicConfigById(id);
				configDo.setStatus(status);
				displayPicConfigService.updateDisplayPicConfig(configDo);
				json.setSuccess(true);
			}
		}catch(Exception e){
			json.setSuccess(false);
		}
		
		return json;
	}
	
	
	@Post("doCheckConfigSelect")
	public Object doCheckConfigSelect(Invocation inv,@Param("outerId") Long outerId,@Param("selectType") String selectType,@Param("status") int status){
		JsonResult json = new JsonResult();
		try{
			if(!(outerId==null)&&StringUtils.isNotEmpty(selectType)){
				DisplaySelectConfigListDo configDo = displayPicConfigService.findDisplaySelectConfig(outerId, selectType);
				if(status==1){
					if(configDo==null){
						configDo = new DisplaySelectConfigListDo();
						configDo.setOuterId(outerId);
						configDo.setSelectType(selectType);
	                }
					displayPicConfigService.addDisplaySelectConfigListDo(configDo);
				}else{
					displayPicConfigService.removeDisplaySelectConfigListDo(configDo.getId());
				}
				json.setSuccess(true);
			}
		}catch(Exception e){
			json.setSuccess(false);
		}
		
		return json;
	}
}
